# -*- coding: utf-8 -*-
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import BaseBackend
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User

import logging

logger = logging.getLogger(__name__)


class CustomBackend(BaseBackend):
    """
    AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend']
    """

    def authenticate(self, request, username=None, password=None, **kwargs):
        user_model = get_user_model()
        try:
            user: User = user_model.objects.get(username=username, password=password)
            # user: User = User.objects.get(username=username, password=password)
            return user
        except user_model.DoesNotExist as e:
            return None

    def get_user(self, user_id):
        user_model = get_user_model()
        try:
            return user_model.objects.get(pk=user_id)
        except user_model.DoesNotExist:
            return None
